package MRDemo;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.math.BigDecimal;

public class reduceDemo extends Reducer<Text, bean, Text, bean> {

    @Override
    protected void reduce(Text key, Iterable<bean> values, Reducer<Text, bean, Text, bean>.Context context) throws IOException, InterruptedException {
        /*
            1.初始化各种数值
            2.自营和三方数据的求和
            3.所有数据的总和
            4.将需要输出的value内容导入到bean类型中
         */
        BigDecimal price_self_total = BigDecimal.ZERO;
        BigDecimal price_other_total = BigDecimal.ZERO;
        BigDecimal price_self, price_other;
        for (bean value : values) {
            price_self = value.getPrice_self();
            price_other = value.getPrice_other();
            price_self_total = price_self_total.add(price_self);
            price_other_total = price_other_total.add(price_other);
        }

//        BigDecimal price_total = price_other_total.add(price_self_total);
        bean bean = new bean(key.toString(), price_self_total, price_other_total);
        context.write(key, bean);
    }
}
